<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/8
 * Time: 17:51
 */

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\KeywordService;
use App\Constant\ErrorConstant;

class KeywordController extends BaseController
{
    protected $service;

    public function __construct(Request $request, KeywordService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 热门关键词列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listKeyword()
    {
        [$flag, $result]    =   $this->service->keywordList();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_LIST_ERROR, $result);
        }
    }

    /**
     * 热门关键词删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteKeyword()
    {
        $validator  =   Validator::make($this->request->post(), [
            'key_id'       =>  'required|integer',
        ], [
            'key_id.required'  =>  '热门关键词ID不能为空',
            'key_id.integer'   =>  '热门关键词ID必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_DELETE_ERROR, $validator->errors()->first());
        }

        $key_id    =   (int) $this->request->post('key_id');

        [$flag, $result]    =   $this->service->keywordDelete($key_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_DELETE_ERROR, $result);
        }
    }

    /**
     * 热门关键词添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addKeyword()
    {
        $validator  =   Validator::make($this->request->all(), [
            'keyword'  =>  'required|string|max:30',
            'sorting'  =>  'required|integer',
        ], [
            'keyword.required' =>  '热门关键词不能为空',
            'keyword.string'   =>  '热门关键词只能为字符',
            'keyword.max'      =>  '热门关键词过长',

            'sorting.required' =>  '热门关键词排序不能为空',
            'sorting.integer'  =>  '热门关键词排序必须为数字',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_ADD_ERROR, $validator->errors()->first());
        }

        $params =   $this->request->post();
        [$flag, $result]    =   $this->service->keywordAdd($params);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_ADD_ERROR, $result);
        }
    }

    /**
     * 编辑热门关键词
     * @return \Illuminate\Http\JsonResponse
     */
    public function editKeyword()
    {
        $validator  =   Validator::make($this->request->post(), [
            'key_id'    =>  'required|integer|exists:mini_hot_keyword,id',
            'keyword'   =>  'required|string|max:30',
            'sorting'   =>  'required|integer'
        ], [
            'key_id.required'   =>  '热门关键词ID不能为空',
            'key_id.integer'    =>  '热门关键词ID必须为整数',
            'key_id.exists'     =>  '热门关键词不存在',
            'keyword.required'  =>  '热门关键词不能为空',
            'keyword.string'    =>  '热门关键词必须为字符',
            'keyword.max'       =>  '热门关键词过长',
            'sorting.required'  =>  '热门关键词排序不能为空',
            'sorting.integer'   =>  '热门关键词排序必须为整数'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_EDIT_ERROR, $validator->errors()->first());
        }

        $key_id     =   (int) $this->request->post('key_id');
        $params     =   $this->request->post();
        [$flag, $result]    =   $this->service->keywordEdit($params, $key_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_KEYWORD_EDIT_ERROR, $result);
        }
    }
}
